{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy import odr\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def linear(B, x):\n",
    "    return B[0]*x + B[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = [10000,0]\n",
    "n_points = 1000\n",
    "\n",
    "x_meas =  np.random.rand(n_points)*2\n",
    "y_meas = np.random.rand(n_points) * 100\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(x_meas,y_meas, c='red',s=100)\n",
    "plt.xlim(-1, 2)\n",
    "plt.ylim(-20, 120)\n",
    "plt.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "B_pred = np.polyfit(x_meas,y_meas, 1)\n",
    "x_pred_line= np.linspace(-5,5)\n",
    "y_pred_line = linear(B_pred, x_pred_line)\n",
    "plt.figure()\n",
    "plt.scatter(x_meas,y_meas, c='red',s=100)\n",
    "plt.plot(x_pred_line, y_pred_line, c= 'orange')\n",
    "plt.xlim(-1, 2)\n",
    "plt.ylim(-20, 120)\n",
    "plt.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "start = time.time()\n",
    "odr_model = odr.Model(linear)\n",
    "data = odr.Data(x_meas, y_meas)\n",
    "ordinal_distance_reg = odr.ODR(data, odr_model,\n",
    "                               beta0=[0.2, 1.],\n",
    "                               maxit=600)\n",
    "out = ordinal_distance_reg.run()\n",
    "line_coeffs = out.beta\n",
    "end = time.time()\n",
    "print(end - start)\n",
    "\n",
    "x_pred_line= np.linspace(-5,5)\n",
    "y_pred_line = linear(line_coeffs, x_pred_line)\n",
    "plt.figure()\n",
    "plt.scatter(x_meas,y_meas, c='red',s=100)\n",
    "plt.plot(x_pred_line, y_pred_line, c= 'orange')\n",
    "plt.xlim(-1, 2)\n",
    "plt.ylim(-20, 120)\n",
    "\n",
    "plt.plot()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out.pprint()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out.res_var"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Cut off line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cut_off(x, y, y_min, y_max, line_coeffs, i):\n",
    "    if y[i] > y_max:\n",
    "        y[i] = y_max\n",
    "        x[i] = 1/line_coeffs[0]*(y_max-line_coeffs[1])\n",
    "    if y[i] < y_min:\n",
    "        y[i] = y_min\n",
    "        x[i] = 1/line_coeffs[0]*(y_min-line_coeffs[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "line_coeffs = np.array([1,1])\n",
    "x_start, x_end = 0.1, 2\n",
    "y_min, y_max = 1.5, 2.5\n",
    "x = np.array([x_start, x_end])\n",
    "line = np.poly1d(line_coeffs)\n",
    "y = line(x)\n",
    "\n",
    "plt.scatter(x, y, marker = 'o', c='red', s=100)\n",
    "\n",
    "print(x)\n",
    "print(y)\n",
    "\n",
    "cut_off(x, y, y_min, y_max, line_coeffs, 0)\n",
    "cut_off(x, y, y_min, y_max, line_coeffs, 1)\n",
    "\n",
    "plt.plot(x, line(x), color='orange')\n",
    "plt.show()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gauge_detection",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
